home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jwpsrc.zip / FORMAT.C < prev    next >
C/C++ Source or Header  |  1993-03-31  |  7KB  |  176 lines

  1. /* Copyright (C) Stephen Chung, 1991-1993.  All rights reserved. */
  2.  
  3. #include "jwp.h"
  4. #include <math.h>
  5.  
  6.  
  7. static struct {
  8.     int spacing;
  9.     int spacemulti;
  10. } NewSettings;
  11.  
  12.  
  13. static BOOL ReformatProc (FILEOPTIONS *f, PARAGRAPH far *p, int n)
  14. {
  15.     p->spacing = NewSettings.spacing;
  16.     p->spacemulti = NewSettings.spacemulti;
  17.  
  18.     return (TRUE);
  19. }
  20.  
  21.  
  22. BOOL FAR PASCAL FormatParagraphProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  23. {
  24.     switch (message) {
  25.         case WM_INITDIALOG: {
  26.             int left, right, first, spacemulti;
  27.             PARAGRAPH far *pp, far *start, far *stop;
  28.             char buffer[MAXLINELEN];
  29.  
  30.             if (SELPARA1(global.active) == NULL) {
  31.                 start = CURPARA(global.active);
  32.                 stop = start->next;
  33.             } else {
  34.                 start = SELPARA1(global.active);
  35.                 stop = SELPARA2(global.active)->next;
  36.             }
  37.  
  38.             left = start->leftindent;
  39.             right = start->rightindent;
  40.             first = start->firstindent - start->leftindent;
  41.             spacemulti = start->spacemulti;
  42.  
  43.             SetDlgItemInt(hwnd, 4201, left, TRUE);
  44.             SetDlgItemInt(hwnd, 4202, right, TRUE);
  45.             SetDlgItemInt(hwnd, 4203, first, TRUE);
  46.  
  47.             for (pp = start; pp != stop; pp = pp->next) {
  48.                 if (left != pp->leftindent) SetDlgItemText(hwnd, 4201, "");
  49.                 if (right != pp->rightindent) SetDlgItemText(hwnd, 4202, "");
  50.                 if (first != (pp->firstindent - pp->leftindent)) SetDlgItemText(hwnd, 4203, "");
  51.                 if (spacemulti != pp->spacemulti) spacemulti = -1;
  52.             }
  53.  
  54.             SendDlgItemMessage(hwnd, 4201, EM_LIMITTEXT, 3, 0L);
  55.             SendDlgItemMessage(hwnd, 4202, EM_LIMITTEXT, 3, 0L);
  56.             SendDlgItemMessage(hwnd, 4203, EM_LIMITTEXT, 3, 0L);
  57.             SendDlgItemMessage(hwnd, 4211, EM_LIMITTEXT, 4, 0L);
  58.  
  59.             if (spacemulti >= 100) {
  60.                 if (spacemulti % 100 == 0) {
  61.                     sprintf(buffer, "%d", spacemulti / 100);
  62.                 } else if (spacemulti % 10 == 0) {
  63.                     sprintf(buffer, "%3.1lf", (double) spacemulti / 100.0);
  64.                 } else {
  65.                     sprintf(buffer, "%4.2lf", (double) spacemulti / 100.0);
  66.                 }
  67.                 SetDlgItemText(hwnd, 4211, buffer);
  68.             } else {
  69.                 SetDlgItemText(hwnd, 4211, "");
  70.             }
  71.  
  72.             CenterDialogBox(hwnd);
  73.             return (TRUE);
  74.         }
  75.  
  76.         case WM_COMMAND:
  77.             switch (wParam) {
  78.                 case IDOK: {
  79.                     int i, left, right, first;
  80.                     double r;
  81.                     PARAGRAPH far *pp, far *start, far *stop;
  82.                     char buffer[MAXLINELEN];
  83.                     char word[MAXLINELEN];
  84.  
  85.                     if (SELPARA1(global.active) == NULL) {
  86.                         start = CURPARA(global.active);
  87.                         stop = start->next;
  88.                         UndoAddFormatParagraph(global.active, global.active->current, global.active->current);
  89.                     } else {
  90.                         start = SELPARA1(global.active);
  91.                         stop = SELPARA2(global.active)->next;
  92.                         UndoAddFormatParagraph(global.active, SEL1(global.active), SEL2(global.active));
  93.                     }
  94.  
  95.                     for (i = 0, pp = start; pp != stop; i++, pp = pp->next) {
  96.                         left = pp->leftindent;
  97.                         right = pp->rightindent;
  98.                         first = pp->firstindent;
  99.  
  100.                         /* Indentations */
  101.  
  102.                         GetDlgItemText(hwnd, 4201, buffer, MAXLINELEN);
  103.                         word[0] = '\0'; sscanf(buffer, "%s", word);
  104.                         if (word[0]) left = atoi(word);
  105.  
  106.                         GetDlgItemText(hwnd, 4202, buffer, MAXLINELEN);
  107.                         word[0] = '\0'; sscanf(buffer, "%s", word);
  108.                         if (word[0]) right = atoi(word);
  109.  
  110.                         GetDlgItemText(hwnd, 4203, buffer, MAXLINELEN);
  111.                         word[0] = '\0'; sscanf(buffer, "%s", word);
  112.                         if (word[0]) first = left + atoi(word);
  113.  
  114.                         if (left >= 256 || right >= 256 || first >= 256) {
  115.                             ErrorMessage(hwnd, "Indentations must be smaller than 255!");
  116.                             return (TRUE);
  117.                         }
  118.                         if (left < 0 || right < 0 || first < 0) {
  119.                             ErrorMessage(hwnd, "Indentations cannot be negative!");
  120.                                 return (TRUE);
  121.                         }
  122.                         if (first + right >= global.active->linelen) {
  123.                             ErrorMessage(hwnd, "Total first-line indentation is larger than "
  124.                                                "the total length of line #%d!", i + 1);
  125.                             return (TRUE);
  126.                         }
  127.                         if (left + right >= global.active->linelen) {
  128.                             ErrorMessage(hwnd, "Total line indentation is larger than "
  129.                                                "the total length of line #%d!", i + 1);
  130.                             return (TRUE);
  131.                         }
  132.  
  133.                         if (left != pp->leftindent) {
  134.                             pp->leftindent = left;
  135.                             global.active->changed = TRUE;
  136.                         }
  137.                         if (right != pp->rightindent) {
  138.                             pp->rightindent = right;
  139.                             global.active->changed = TRUE;
  140.                         }
  141.                         if (first != pp->firstindent) {
  142.                             pp->firstindent = first;
  143.                             global.active->changed = TRUE;
  144.                         }
  145.                     }
  146.  
  147.                     /* Line spacing */
  148.  
  149.                     GetDlgItemText(hwnd, 4211, buffer, MAXLINELEN);
  150.                     word[0] = '\0'; sscanf(buffer, "%s", word);
  151.                     if (word[0]) {
  152.                         r = atof(word);
  153.                         if (r < 1.0) r = 1.0;
  154.                         NewSettings.spacemulti = (int) floor(r * 100.0 + 0.5);
  155.  
  156.                         NewSettings.spacing = (global.active->basefont->height + global.active->spacing) * r;
  157.                         NewSettings.spacing -= global.active->basefont->height;
  158.  
  159.                         SetReformatProc(ReformatProc);
  160.                         global.active->changed = TRUE;
  161.                     }
  162.  
  163.                     EndDialog(hwnd, TRUE);
  164.                     return (TRUE);
  165.                 }
  166.  
  167.                 case IDCANCEL:
  168.                     EndDialog(hwnd, FALSE);
  169.                     return (TRUE);
  170.             }
  171.             break;
  172.     }
  173.  
  174.     return (FALSE);
  175. }
  176.